package com.nextStep.utils;


import com.nextStep.entity.SendEmailUser;
import com.nextStep.service.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

/**
 * 发送邮件
 */
public class SendEmail {
    final private String account="bnahlz@163.com";
    final private String password="abc123";
    final private static  Logger logger = LoggerFactory.getLogger(SendEmail.class);


    private MimeMessage m =null;
    private Session session=null;
    private Properties config=null;

    /**
     * 发送邮件
     * @throws Exception
     */
    public  boolean sendEmailTO(SendEmailUser sendEmailUser) throws Exception {

        try{
            m=createEmail(sendEmailUser);
            session=m.getSession();
            session.setDebug(true);
            config=session.getProperties();
            //config.put("mail.transport.protocol", "smtp");
            config.put("mail.smtp.host", "smtp.163.com");
            //config.put("mail.smtp.port", "25");  //阿里云行不通

            config.put("mail.smtp.port", "465");
            config.put("mail.smtp.ssl.enable", true);   //465需要开启

            config.put("mail.smtp.auth", "true");
            //config.put("mail.smtp.ssl", "false");

            Transport.send(m);
        }catch (Exception e){
            throw new Exception(e);
        }
        return true;

    }


    /**
     * 创建一封邮件
     * @param sendEmailUser    邮件
     * @return
     * @throws Exception
     */
    private   MimeMessage createEmail(SendEmailUser sendEmailUser) throws Exception{
        Properties props=new Properties();

        Session session=Session.getInstance(props,new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(account, password);
            }

        });
        //创建邮件对象
        MimeMessage m=new MimeMessage(session);
        //设置发件人
        Address from =new InternetAddress(account ,sendEmailUser.getSendUserName(),"utf-8");
        m.setFrom(from);
        //收件人
        m.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(sendEmailUser.getTo()));
        //设置抄送人
        if(sendEmailUser.getCc()!=null  ){

            m.setRecipient(RecipientType.CC, new InternetAddress(sendEmailUser.getCc()));
        }
        //设置密送
        if(sendEmailUser.getBcc()!=null  ){

            m.setRecipient(RecipientType.BCC, new InternetAddress(sendEmailUser.getBcc()));
        }

        //设置邮件主题
        m.setSubject(sendEmailUser.getTheme(),"utf-8");
        //设置邮件正文
        m.setContent(sendEmailUser.getText(),"text/plain;charset=utf-8");
        //设置邮件发送时间
        m.setSentDate(new Date());
        //确认设置完毕
        m.saveChanges();

        return m;


    }
}
